﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace ProductManagement
{
    public partial class BanVo : Form
    {
        DataTable dtLoaiGas;
        DataTable dtVoBinhGas;
        public BanVo()
        {
            InitializeComponent();

            dtVoBinhGas = new DataTable();
            dtVoBinhGas.Columns.Add("STT", typeof(int));
            dtVoBinhGas.Columns.Add("MaVo", typeof(int));
            dtVoBinhGas.Columns.Add("LoaiVo", typeof(String));
            dtVoBinhGas.Columns.Add("SoLuong", typeof(int));
        }

        private void dgvVoBinhGas_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvVoBinhGas.SelectedRows.Count > 0)
            {
                btnXoa.Enabled = true;
            }
            else
                btnXoa.Enabled = false;
        }

        private void BanVo_Load(object sender, EventArgs e)
        {
            try
            {
                String strSql = "select vo.LoaiGas as MaVo, loai.TenLoai &' ('&sum(vo.SoLuong)&')' as LoaiVo, sum(vo.SoLuong) as SoLuong from VoBinhGas vo, LoaiGas loai where vo.LoaiGas=loai.ID group by vo.LoaiGas, loai.TenLoai";
                dtLoaiGas = DataAccess.Query(strSql);
                cboLoaiGas.DisplayMember = "LoaiVo";
                cboLoaiGas.ValueMember = "MaVo";
                cboLoaiGas.DataSource = dtLoaiGas;
                dgvVoBinhGas.AutoGenerateColumns = false;
                dgvVoBinhGas.DataSource = dtVoBinhGas;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }

        private void btnBanVo_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow aRow in dtVoBinhGas.Rows)
                {
                    int soLuong = (int)aRow["SoLuong"];
                    int loaiVo = (int)aRow["MaVo"];
                    VoBinhGasBUS.BanVo(loaiVo, soLuong);
                }                
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch (Exception ex) {
                MessageBox.Show("Cập nhật dữ liệu không thành công\r\n"+ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private String GetTenGas(String str)
        {
            int i1 = str.LastIndexOf('(');
            if (i1 >= 0)
            {
                int i2 = str.LastIndexOf(')');
                return str.Substring(0, i1 - 1);
            }
            return str;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (cboLoaiGas.SelectedIndex >= 0)
            {
                int maVo = (int)cboLoaiGas.SelectedValue;
                int soLuong = int.Parse(txtSoLuong.Value.ToString());
                int soVoTon = 0;
                foreach (DataRow aRow in dtLoaiGas.Rows)
                {
                    if ((int)aRow["MaVo"] == maVo)
                        soVoTon += int.Parse(aRow["SoLuong"].ToString());
                }
                DataRow curRow = null;
                foreach (DataRow aRow in dtVoBinhGas.Rows)
                {
                    if ((int)aRow["MaVo"] == maVo)
                    {
                        soLuong += (int)aRow["SoLuong"];
                        curRow = aRow;
                        break;
                    }
                }
                if (soVoTon < soLuong)
                {
                    MessageBox.Show("Không đủ vỏ " + GetTenGas(cboLoaiGas.Text) + " để đổi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (curRow == null)
                    {
                        DataRow newRow = dtVoBinhGas.NewRow();                        
                        newRow["STT"] = dtVoBinhGas.Rows.Count + 1;
                        newRow["MaVo"] = maVo;
                        newRow["LoaiVo"] = GetTenGas(cboLoaiGas.Text);
                        newRow["SoLuong"] = soLuong;
                        dtVoBinhGas.Rows.Add(newRow);
                        btnBanVo.Enabled = true;
                    }
                    else
                        curRow["SoLuong"] = soLuong;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvVoBinhGas.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow aRow in dgvVoBinhGas.Rows)
                {
                    dtVoBinhGas.Rows.RemoveAt(aRow.Cells[0].RowIndex);
                }
            }
            if (dtVoBinhGas.Rows.Count == 0)
                btnBanVo.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
